<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>WebSocket 测试</title>
</head>
<body>
<h2>WebSocket 客户端</h2>
<input type="text" id="messageInput" placeholder="输入要发送的消息">
<button onclick="sendMessage()">发送</button>

<script>
    // 创建 WebSocket 连接（请替换为你的 WebSocket 地址）
    const socket = new WebSocket('ws://localhost:8083');

    // 连接建立时触发
    socket.addEventListener('open', function (event) {
        console.log('连接成功');
    });

    // 监听接收消息事件
    socket.addEventListener('message', function (event) {
        console.log('收到消息:', event.data);
    });

    // 连接关闭时触发
    socket.addEventListener('close', function (event) {
        console.log('连接关闭');
    });

    // 发送消息函数
    function sendMessage() {
        const input = document.getElementById('messageInput');
        const message = input.value;
        if (message && socket.readyState === WebSocket.OPEN) {
            socket.send(message);
        } else {
            console.warn('WebSocket 未连接或消息为空');
        }
    }
</script>
</body>
</html>
